package com.ibl.rover.lite.handler;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ibl.rover.lite.service.utill.Core.APPSETUP;
import com.ibl.rover.lite.service.utill.HibernateUtill;
import com.ibl.rover.lite.service.utill.Core.SST_TAGS;
import com.rover.lite.hibernate.dal.RvrAppsetup;
import com.ibl.rover.lite.service.utill.Core;
 
import com.ibl.rover.lite.service.sync.request.implementation.SyncRequestHandler;
public class AppSetupHandler {

	Session session;
	Document doc;
	public AppSetupHandler(Document _doc) {
		// TODO Auto-generated constructor stub
			session = HibernateUtill.getSessionFactory().openSession();
		 doc=_doc;
		 
	}
	
	
	public void getAppSetup(Element bodyElement,String userName) {
		
		Criteria rvrAppsetupCriteria = session.createCriteria(RvrAppsetup.class);
		rvrAppsetupCriteria.add(Restrictions.eq("id.tsoCode", userName));
		List<RvrAppsetup>  listAppSetup = new ArrayList<RvrAppsetup>();
		listAppSetup=rvrAppsetupCriteria.list();

		Element entityElement = doc.createElement("entity");
		entityElement.setAttribute("name", SST_TAGS.APPSETUP);
		bodyElement.appendChild(entityElement);

		Element addElement = doc.createElement("add");
		entityElement.appendChild(addElement);
		
		
		for (RvrAppsetup rvrAppsetup : listAppSetup) {
			Element itemElement = doc.createElement("item");
			addElement.appendChild(itemElement);
			setRvrAppsetupAttribute(rvrAppsetup, itemElement);

		}

		
	}



	private void setRvrAppsetupAttribute(RvrAppsetup rvrAppsetup,
			Element itemElement) {
		
		try {
	 		ArrayList<String> columnName = new ArrayList<String>();
			ArrayList<String> columnValue = new ArrayList<String>();

			columnName.add(APPSETUP.LAST_ORDER_NO);
			columnValue.add(String.valueOf(rvrAppsetup.getId().getLastOrderNo()));

			columnName.add(APPSETUP.TSO_NAME);
			columnValue.add(String.valueOf(rvrAppsetup.getId().getTsoCode()));

		Core.setAttribute(columnName, columnValue, itemElement,doc);
	 	} catch (Exception e) {
		 
		}
		
	}

	
}
